package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("strategyTravel")
public class StrategyTravelController {


    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyService strategyService;
    // 大家都在看
    @RequestMapping("list")
    public String list(Model model ,Long strategyId){
        List<Travel> list = travelService.selectTravelsBystrategyId(strategyId);
        model.addAttribute("strategyId",strategyId);
        // 根据攻略id查询到的数据
        model.addAttribute("list",list);
        model.addAttribute("strategies",strategyService.selectAll());
        return "strategyTravel/list";
    }
    // 删除
    @RequestMapping("delete")
    public String delete(Long strategyId,Long travelId){
        travelService.deleteRelation(strategyId,travelId);
        return "redirect:/strategyTravel/list.do?strategyId=" + strategyId;
    }

    // 查询与攻略同地区的游记
    @RequestMapping("getRetionTravelByStrategyId")
    @ResponseBody
    public Object getRetionTravelByStrategyId(Long strategyId){
        return  travelService.getRegionTravelByStrategyId(strategyId);
    }

    @RequestMapping("save")
    @ResponseBody
    public JSONResult saveOrUpdate(Long strategyId,Long travelId){
        JSONResult result = new JSONResult();
        try {
            travelService.saveRelation(strategyId,travelId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
